#include <iostream>
using namespace std;
static bool wrong = false;
int readNumbers(const char arr[],int idxbg,int idxed)
{
	if(idxbg > idxed)
	{
		wrong = true;
		return 0;
	}
	int sum = 0, bit = 1;
	for(int idx = idxed; idx >= idxbg;--idx)
	{
		if(arr[idx] >= '0' && arr[idx] <= '9')
		{
			sum+=(arr[idx] - '0')*bit;
			bit*=10;
		}
		else
		{
			wrong = true;
			return 0;
		}
	}
	cout<<"sum : "<<sum<<endl;
	return sum;
}

int atoi(const char arr[], int length)
{
	if(arr == nullptr || length <=0)
	{
		wrong = true;
		return 0;
	}
	if(arr[0] == '-')
		return -readNumbers(arr,1,length-1);
	else if(arr[0] == '+')
		return readNumbers(arr,1,length-1);
	else if(arr[0] >= '0' && arr[0] <= '9')
		return readNumbers(arr,0,length-1);
	else
	{
		wrong = true;
		return 0;
	}
}

int main()
{
	cout<<atoi("123",3)<<endl;
	cout<<(wrong?"wrong":"right")<<endl;
	cout<<atoi("-123",4)<<endl;
	cout<<(wrong?"wrong":"right")<<endl;
	cout<<atoi("-0",2)<<endl;
	cout<<(wrong?"wrong":"right")<<endl;
	cout<<atoi("-0",2)<<endl;
	cout<<(wrong?"wrong":"right")<<endl;
	cout<<atoi("2147483647",10)<<endl;
	cout<<(wrong?"wrong":"right")<<endl;
	cout<<atoi("-2147483648",11)<<endl;
	cout<<(wrong?"wrong":"right")<<endl;
	cout<<atoi("99999999999",11)<<endl;
	cout<<(wrong?"wrong":"right")<<endl;
	cout<<atoi(nullptr,11)<<endl;
	cout<<(wrong?"wrong":"right")<<endl;

	cout<<atoi("",0)<<endl;
	cout<<(wrong?"wrong":"right")<<endl;

	cout<<atoi("012abc",6)<<endl;
	cout<<(wrong?"wrong":"right")<<endl;

	return 0;
}